package lastmpd;

/*
 * Copyright (C) 2010 Mark Breddemann
 * 
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software Foundation;
 * either version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this
 * program; if not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.roarsoftware.lastfm.Album;
import net.roarsoftware.lastfm.Artist;
import net.roarsoftware.lastfm.Caller;
import net.roarsoftware.lastfm.Period;
import net.roarsoftware.lastfm.Track;
import net.roarsoftware.lastfm.User;

public class LastfmConnection {

	private final Log log = LogFactory.getLog(this.getClass());
	private final static String apiKey = "af8ebbe206bc4fac9fbc7ac26cd7b79a";
	
	public LastfmConnection() {
		log.debug("creating lastfm connection");
		Caller.getInstance().setUserAgent("lastmpd");
//		Caller.getInstance().setDebugMode(true);
	}
	
	
	
	public ArrayList<LastfmSong> getTopTracksFromArtist(String artist, int count) {
		log.debug("fetching top tracks from " + artist);
		Collection<Track> tracks = Artist.getTopTracks(artist, apiKey);
		return toLastfmSongArrayList(tracks, count);
	}
	
	public ArrayList<LastfmSong> getTopTracksFromSimilarArtists(String artist, int similarArtistsCount, int topTracksPerArtist) {
		ArrayList<LastfmSong> result = new ArrayList<LastfmSong>(similarArtistsCount*topTracksPerArtist);
		Collection<Artist> artists = Artist.getSimilar("artist", similarArtistsCount, apiKey);
		log.debug("got " + artists.size() + " similar artists");
		int ca = 0;
		for (Artist fmArtist: artists) {
			if (ca++ == similarArtistsCount) break;
			
			Collection<Track> topTracks = Artist.getTopTracks(fmArtist.getName(), apiKey);
			
			int ct = 0;
			for (Track track: topTracks) {
				if (ct++ < topTracksPerArtist) { result.add(new LastfmSong(track.getArtist(), track.getName(), track.getAlbum())); }
			}
		}
		return result;
	}
	
	public ArrayList<LastfmSong> getTopTracksFromUser(String user, int count) {
		Collection<Track> tracks = User.getTopTracks(user, Period.TWELVE_MONTHS, apiKey);
		return toLastfmSongArrayList(tracks, count);
	}
	
	public ArrayList<LastfmSong> getLovedTracks(String user, int count) {
		Collection<Track> tracks = User.getLovedTracks(user, apiKey);
		return toLastfmSongArrayList(tracks, count);
	}
	
	public ArrayList<Album> getTopAlbumsFromArtist(String artist, int count) {
		int c = 0;
		ArrayList<Album> result = new ArrayList<Album>(count);
		Collection<Album> albums = Artist.getTopAlbums(artist, apiKey);
		
		for (Album album: albums) {
			if (c++ == count) break;
			result.add(album);
		}
		return result;
	}
	
	public ArrayList<String> getSongsFromAlbum(Album album) {
		Album album2 = Album.getInfo(album.getArtist(), album.getName(), apiKey);
		if (album2 == null) {
			log.warn("could get album info for " + album.getArtist() + " - " + album.getName());
			return null;
		}
		return album2.getTracks();
	}
	
	
	
	private ArrayList<LastfmSong> toLastfmSongArrayList(Collection<Track> original, int count) {
		ArrayList<LastfmSong> a = new ArrayList<LastfmSong>(count);
		int c = 0;
		for (Track track: original) {
			if (c++ == count) break;
			a.add(new LastfmSong(track.getArtist(), track.getName(), track.getAlbum()));
		}
		return a;
	}
	
}
